package com.zhl.media.app.mvp.presenter;

import android.app.Application;
import android.os.Environment;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.app.mvp.contract.FileUploadContract;
import com.zhl.media.commonsdk.base.BaseRespose;
import com.zhl.media.commonsdk.entity.MyPushPicture;
import com.zhl.media.commonsdk.utils.FileUploadObserver;
import com.zhl.media.commonsdk.utils.UploadFileRequestBody;

import java.io.File;
import java.util.List;
import javax.inject.Inject;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import okhttp3.MultipartBody;
import okhttp3.ResponseBody;


@ActivityScope
public class FileUploadPresenter extends BasePresenter<FileUploadContract.Model, FileUploadContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public FileUploadPresenter(FileUploadContract.Model model, FileUploadContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

        public void upload(MediaPhoto mediaPhoto, int postion) {
//        File file1 = new File(mediaPhoto.getPhotoPath());
        File file1 = new File(Environment.getExternalStorageDirectory().toString() + "/a.exe");

        UploadFileRequestBody uploadFileRequestBody = new UploadFileRequestBody(file1, new FileUploadObserver<ResponseBody>() {
            @Override
            public void onUploadSuccess(ResponseBody responseBody) {

            }

            @Override
            public void onUploadFail(Throwable e) {

            }

            @Override
            public void onProgress(int progress) {

            }
        });

        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.addFormDataPart("mediaId",mediaPhoto.getMediaId());
        MultipartBody.Builder builder1 = builder.setType(MultipartBody.FORM);
        builder1.addFormDataPart("file", file1.getName(), uploadFileRequestBody);
        List<MultipartBody.Part> part = builder.build().parts();

//        mModel.updateResultFile(part)
//                .observeOn(AndroidSchedulers.mainThread())
//                .doFinally(() -> {
//                    mRootView.onUploadStart(postion);
//                })
//                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
//                .subscribe(new ErrorHandleSubscriber<BaseRespose<String>>(mErrorHandler) {
//                    @Override
//                    public void onNext(BaseRespose<String> baseRespose) {
//                        mRootView.onUploadSuccess(postion);
//                    }
//                });
    }


    public void currentPushImgList(String s) {
        mModel.currentPushImgList(s)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseRespose<List<MyPushPicture>>>(mErrorHandler) {
                    @Override
                    public void onNext(BaseRespose<List<MyPushPicture>> data) {
                        mRootView.onSuccess(data.data);
                    }
                });
    }
}
